package com.ikun.controller;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.ikun.entity.Users;
import com.ikun.service.AttendancesService;
import com.ikun.service.BuildingsService;
import com.ikun.service.SalariesService;
import com.ikun.service.UserService;
import com.ikun.util.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @ClassName: AdminStuffController
 * @Author: 吴思伟
 * @CreateTime: 2025-03-26 14:53
 * @Description: TODO
 */
@Controller
@RequestMapping("/admin/stuff")
@Tag(name = "管理员-管家-工人管理相关", description = "管理员-管家-工人管理相关")
public class AdminStuffController {
    @Autowired
    private UserService userService;

    @Autowired
    private SalariesService salariesService;

    @Autowired
    private AttendancesService attendancesService;

    @Autowired
    private BuildingsService buildingsService;

    @GetMapping("/allManage")
    @ResponseBody
    @Operation(summary = "查询所有管家信息")
    public Result showAllManage() {
        return Result.success(userService.findAllMangerDetail());
    }

    @PutMapping("/updateManage")
    @ResponseBody
    @Operation(summary = "更新管家信息")
    public Result updateManage(@RequestBody Users users) {
        String msg = userService.updateUser(users);
        return Result.success(msg);
    }

    @DeleteMapping("/deleteManage")
    @ResponseBody
    @Operation(summary = "删除管家信息")
    public Result deleteManage(Integer userId) {
        String msg = userService.deleteUser(userId);
        return Result.success(msg);
    }

    @PostMapping("/addManage")
    @ResponseBody
    @Operation(summary = "添加管家信息")
    public Result addManage(String userName, String password, String phone) {
        Users users = new Users();
        users.setName(userName);
        users.setPassword(password);
        users.setPhone(phone);
        System.out.println("users = " + users);
        String msg = userService.addManger(users);
        return Result.success(msg);
    }

    @GetMapping("/allMangerAttendances")
    @ResponseBody
    @Operation(summary = "查询所有管家考勤信息")
    public Result showAllMangerAttendances(@RequestParam(required = false) String datePart,
        @RequestParam(required = false) String buildPart) {
        return Result.success(userService.findAllMangerAttendances(datePart, buildPart));
    }

    @PostMapping("/managers/attendance")
    @ResponseBody
    @Operation(summary = "查询管家考勤信息")
    public Result calculateSalaryForManger(String manageId) {
        // 当前年月
        YearMonth currentYearMonth = YearMonth.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 格式化日期
        String formattedYearMonth = currentYearMonth.format(formatter);
        return Result.success(salariesService.calculateSalaryForManger(formattedYearMonth, manageId));
    }

    @PostMapping("/managers/salary")
    @ResponseBody
    @Operation(summary = "通过名字模糊查询管家工资信息")
    public Result calculateSalaryForMangerByName(String manageName) {
        return Result.success(salariesService.calculateSalaryForMangerByName(manageName));
    }

    @GetMapping("/allStuff")
    @ResponseBody
    @Operation(summary = "查询所有工人信息")
    public Result showAllStuff() {
        return Result.success(userService.findAllStuff());
    }

    @PostMapping("/updateStuff")
    @ResponseBody
    @Operation(summary = "更新工人信息")
    public Result updateStuff(@RequestBody Users users) {
        String msg = userService.updateUser(users);
        return Result.success(msg);
    }

    @DeleteMapping("/deleteStuff")
    @ResponseBody
    @Operation(summary = "删除工人信息")
    public Result deleteStuff(Integer userId) {
        String msg = userService.deleteUser(userId);
        return Result.success(msg);
    }

    @PostMapping("/addStuff")
    @ResponseBody
    @Operation(summary = "添加工人信息")
    public Result addStuff(String userName, String password, String phone) {
        Users users = new Users();
        users.setName(userName);
        users.setPassword(password);
        users.setPhone(phone);
        System.out.println("users = " + users);
        String msg = userService.addStaff(users);
        return Result.success(msg);
    }

    @GetMapping("/allStuffAttendances")
    @ResponseBody
    @Operation(summary = "查询所有工人考勤信息")
    public Result showAllStuffAttendances(@RequestParam(required = false) String datePart,
        @RequestParam(required = false) String namePart) {
        return Result.success(attendancesService.findAllStuffAttendances(datePart, namePart));
    }

    @PostMapping("/stuff/salary")
    @ResponseBody
    @Operation(summary = "查询工人工资信息")
    public Result calculateSalaryForStuff(String stuffId) {
        // 当前年月
        YearMonth currentYearMonth = YearMonth.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 格式化日期
        String formattedYearMonth = currentYearMonth.format(formatter);
        return Result.success(salariesService.calculateSalaryForStuff(formattedYearMonth, stuffId));
    }

    @PostMapping("/builds/count")
    @ResponseBody
    public Result countBuilds() {
        return Result.success(buildingsService.count());
    }

    @GetMapping("/buildsDetails")
    @ResponseBody
    @Operation(summary = "查询所有楼栋信息")
    public Result buildsDetails() {
        return Result.success(buildingsService.buildsDetails());
    }

    @PostMapping("/count")
    @ResponseBody
    public Result countAllStuff() {
        return Result.success(userService.countAllStuffs());
    }

    @PostMapping("/updateManageBuilds")
    @ResponseBody
    @Operation(summary = "更新管家负责的楼栋信息")
    public Result updateManageBuilds(Integer managerId, String[] buildingIds) {
        return buildingsService.updateManageBuilds(managerId, buildingIds);
    }

}
